/**********************************************************************
 * 函数名称： // path_handle
 * 功能描述： // 改变路径格式
 * 访问的表： // fileInfo
 * 修改的表： // NULL
 * 输入参数： // const char* p_pa, char* p_path
 * 返回的值： // 正确返回修改后的路径字符串， 错误返回-1
 * 更新说明： // 
 * 修改日期        版本号     修改人          修改内容
 * -----------------------------------------------
 * 2023/04/16       V3.2       zhao            XXXX
 ***********************************************************************/
#include <func.h>

int mysql(const char *p_user, char *buf, int b){
    MYSQL * mysql = mysql_init(NULL); // 为mysql连接分配内存（线程不安全）

    MYSQL * ret = mysql_real_connect(mysql,"localhost", "root", "123456", "test", 0, NULL, 0);  //密码、数据库名可能不同
    if(ret == NULL){
        return -1;
    }
    char sql[1024] = {0};
    sprintf(sql,"select * from fileInfo WHERE user = '%s';", p_user); 
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        return -1;
    }

    MYSQL_RES *result = mysql_store_result(mysql); //取出读取的结果
    MYSQL_ROW row, row2; //MYSQL_ROW 为char**
    char info[1024] = {0};
    
    while((row = mysql_fetch_row(result)) != NULL){
        switch(b){
        case 1:
            if(strcmp(row[8], "curpath") == 0){
                sprintf(info, "%s/%s", row[7], row[1]);
                strcat(info, buf);
                strcpy(buf, info);
                return 0;    
                }
            break;
        case 2:
            if(strcmp(row[8], "lastpath") == 0){
                sprintf(info, "%s/%s", row[7], row[1]);
                strcat(info, buf);
                strcpy(buf, info);
                return 0;
            }
            break;
        case 3:
            if(atoi(row[3]) == -1) {
                sprintf(info, "/%s", row[1]);
                strcat(info, buf);
                strcpy(buf, info);
                return 0;
            }
            break;
        }

    }
    return -1;
}

char *move(char * cur){
    for (size_t i = 1; i < strlen(cur); ++i){
        cur[i - 1] = cur[i];
    }
    cur[strlen(cur) - 1] = 0;
    return cur;
}

char* path_handle(const char *p_user, char* p_path) {
    if(strlen(p_path) == 0 || p_path[0] == '~' || p_path[0] == '-' ) {
        return p_path;
    }else {
        char buf[1000] = {0};
        strcpy(buf, p_path);
        bzero(p_path, strlen(p_path));
        p_path[0] = '1';
        
        if( buf[0] == '.' ){
            if (buf[1] == '.') {
                move(buf);
                move(buf);
                int a = mysql(p_user, buf, 2);
                if(a == 0){
                    strcpy(p_path, buf);
                }
                return p_path;
            }else {
                move(buf);
                int a = mysql(p_user, buf, 1);
                if(a == 0){
                    strcpy(p_path, buf);
                }
                return p_path;   
            }
        }else if(buf[0] == '/') {
            int a = mysql(p_user, buf, 3);
            if(a == 0){
                strcpy(p_path, buf);
            }
            return p_path;
        }else {
            char buf1[1000] = {0};
            buf1[0] = '/'; 
            strcat(buf1, buf);
            strcpy(buf, buf1);
            int a = mysql(p_user, buf, 1);
            if(a == 0){
                strcpy(p_path, buf);
            }
            return p_path;   
        }     
    }
}
